'use strict';

//注册屏幕缩放时候的处理方法
(function($){

  var resizeRegisters = [];
  var documentReadyRegisters = [];
  var documentReadyFlag = 0;

  var redrawRegisters = {
    register: function(callback){
      resizeRegisters.push(callback);
    },
    registerDocumentReady: function(callback){
      if(documentReadyFlag === 0){
        documentReadyRegisters.push(callback);
      }else{
        callback.call();
      }
    }
  };

  window.onresize  = function(){
    for(var i = 0 ; i < resizeRegisters.length; i++){
      resizeRegisters[i].call();
    }
  };

  $(document).ready(function(){
    documentReadyFlag = 1;
    for(var i = 0 ; i < documentReadyRegisters.length; i++){
      documentReadyRegisters[i].call();
    }
  });

  window.redrawRegisters = redrawRegisters;

})(jQuery);


//page
var pageIndex = 0;
var pageElemnts = $('.page-content li');

var gotoNextPage = function(){
  pageIndex ++;
  $(pageElemnts[pageIndex]).addClass('active animated slideInUp');//
  setTimeout(function(){
    $(pageElemnts[pageIndex - 1]).removeClass('active');
  }, 1000);
};

$('.need-parent').click(function(){
  gotoNextPage();
});
$('.need-child').click(function(){
  gotoNextPage();
});

var clientWidth = $(window).width() > 640 ? 640 : $(window).width();
var clientHeight = $(window).height();

$('div.img-cover').each(function() {
  var that = $(this);
  var curImgWidth = that.attr('data-width')? parseInt(that.attr('data-width')) :650,
    curImgHeight = that.attr('data-height')? parseInt(that.attr('data-height')) :310;

  var tempHeight = (curImgHeight / curImgWidth) * clientWidth;
  tempHeight = tempHeight > clientHeight / 2 ? clientHeight / 2 : tempHeight;

  var $scrollEles = $('.can-scroll');
  $scrollEles.each(function(){
    var _$this = $(this);
    if(_$this.hasClass('result-can-scroll')){
      _$this.height(clientHeight - 50);//底部的菜单高度为50px
    }else{
      _$this.height(clientHeight - tempHeight - 50);//底部的菜单高度为50px
    }

  });


  //$('.can-scroll .bar').each(function(){
  //  var $this = $(this);
  //  $this.css({
  //    top : $this.position().top + ' !important'
  //  });
  //});

  that.css({
    height: tempHeight + 'px'
  });

  var coverImgs = that.find('img');
  coverImgs.each(function(){
    var _$this = $(this);
    _$this.css({
      width: _$this.hasClass('choice')?'25%':'100%', //四个选项横排
      height: tempHeight + 'px'
    });
  });

});


//答案的点击事件
var answersEleemnt = $('div[main-answers] > div');
answersEleemnt.each(function(){
  $(this).mousedown(function(){
    var oneAnswer = $(this);

    //console.log('answersEleemnt',$(this));
    //清空该题目已有的选择
    oneAnswer.parent().find('.selected').removeClass('selected');
    oneAnswer.addClass('selected');


    var $parent  = oneAnswer.parent().parent().parent().parent();
    //显示滑块部分
    $parent.find('.sliderBox').removeClass('hide').addClass('animated zoomIn');
    $parent.find('.continue-btn-wrap').removeClass('unhighlight').addClass('highlight');
  });
});

$('.continue-btn-wrap').each(function(){
  $(this).click(function(){
    if($(this).hasClass('highlight')){
      gotoNextPage();
    }
  });
});

function saveResult(data){
  var psTime = (new Date()).getTime();
  $.post(window.resultUrl, {
      token: window.yoparentToken,
      createTime: window.yoparentStartTime,
      result: JSON.stringify(data),
      quizId: window.yoparentDataId
    },
    function(ret) {
      if (ret === null) {
        window.alert('服务器错误，请稍后重试！');
        return;
      }
      if (ret.status === false) {
        window.location.reload();
        return;
      }
      //var resultPageDesc = window.roundDataUtil.getNext(answerResult[pageCount - 1], answerResult);

      var curTime = (new Date()).getTime();
      var deltaTime = curTime - psTime;
      deltaTime = deltaTime < 1500 ? 1500 : deltaTime;
      deltaTime = deltaTime > 2000 ? 0 : deltaTime;
      //setTimeout(function() {
      //    showResult(resultPageDesc);
      //  },
      //  deltaTime);
    });
}

$('.result-btn-wrap').click(function(){
  if($(this).hasClass('highlight')){
    //获得用户的结果
    var selectInde = parseInt($($('[data-qid]')[0]).attr('data-selectindex'));
    var $resultCotent = $('.result-content');
    $($resultCotent[selectInde]).removeClass('hide');

    saveResult({'q1':selectInde});

    if ( !! window.wxReady) {
      console.log(!!window.yoparentWxTimeineConfig);

      window.yoparentWxConfig.title = $($resultCotent[selectInde]).find('.result-title').attr('data-share-title');
      window.yoparentWxTimeineConfig.title = $($resultCotent[selectInde]).find('.result-title').attr('data-share-title');
      window.yoparentWxConfig.imgUrl = $($resultCotent[selectInde]).find('.img-cover img').attr('src');
      window.yoparentWxTimeineConfig.imgUrl = $($resultCotent[selectInde]).find('.img-cover img').attr('src');
      window.wx.onMenuShareTimeline(window.yoparentWxTimeineConfig);
      window.wx.onMenuShareAppMessage(window.yoparentWxConfig);

    }


    gotoNextPage();
  }
});

setTimeout(function(){
  $('.can-scroll').each(function(){
    var _$this = $(this);
    var _$yScroll = _$this.find('.ps-scrollbar-y-rail');
    _$this.perfectScrollbar({
      stopPropagationOnClick :false
    });
    _$this.on('ps-scroll-y', function(){
      _$this.find('.ps-scrollbar-y-rail').css('opacity', '0.6');

    });
    _$this.on('ps-move-end',function(){
      _$this.find('.ps-scrollbar-y-rail').animate({'opacity':'0'},200);
    });

  });
}, 200);



var orientationFlags = false;
window.onorientationchange = function(){
  switch (window.orientation) {
    case 0:
      if (orientationFlags) {
        $('.orientation').hide();
        orientationFlags = false;
      }
      break;
    case - 90 : if (!orientationFlags) {
      $('.orientation').show();
      orientationFlags = true;
    }
      break;
    case 90:
      if (!orientationFlags) {
        $('.orientation').show();
        orientationFlags = true;
      }
      break;
    case 180:
      if (orientationFlags) {
        $('.orientation').hide();
        orientationFlags = false;
      }
      break;
  }

};

$('.share-btn-wrap').click(function(){
         $(".ending").removeClass("hide").addClass("show").addClass("animated").addClass("fadeIn");
         eventLogger && eventLogger.log("share_btn", "click", "share_btn");
         setTimeout(function() {
                 $(".ending").removeClass("show").removeClass("fadeIn").addClass("fadeOut");
                 setTimeout(function() {
                         $(".ending").addClass("hide").removeClass("fadeOut");
                     },
                     1000);
             },
             1500);
     });

$(document).bind('touchstart',
  function() {});
$(document).bind('touchmove',
  function(event) {
    event.preventDefault();
  });
$(document).bind('touchend',
  function() {});
